/*************************************************************
 * Title: 
 * Description: 
 * Author: Snow Huang
 * Email: Shaobin.Software@gmail.com Shaobin_Software@163.com
 * CreateTime: Aug 6, 2010 3:48:30 PM
 * Copyright © 落羽社区 All right reserved
 ************************************************************/
package snow.core.util.toolkit;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;

/**
 * 
 */
public class CollectionHelper {

	public static <E> boolean contains(Collection<E> collection, E element) {
		return CollectionUtils.isEmpty(collection) ? false : collection.contains(element);
	}

	public static <E> List<E> getNewListFrom(Collection<?> collection, Class<E> clazz) {
		if (CollectionUtils.isEmpty(collection) || clazz == null) {
			return Collections.emptyList();
		}
		List<E> list = new ArrayList<E>(collection.size());
		int index = 0;
		for (Object obj : collection) {
			if (obj != null && clazz.isAssignableFrom(obj.getClass())) {
				list.add(index++, clazz.cast(obj));
			}
		}
		return list;
	}

	public static <E> List<E> getNewListFrom(Collection<E> collection) {
		if (CollectionUtils.isEmpty(collection)) {
			return Collections.emptyList();
		}
		return new ArrayList<E>(collection);
	}

	public static <E> Set<E> getNewSetFrom(Collection<?> collection, Class<E> clazz) {
		if (CollectionUtils.isEmpty(collection) || clazz == null) {
			return Collections.emptySet();
		}
		Set<E> set = new HashSet<E>(collection.size());
		for (Object obj : collection) {
			if (obj != null && clazz.isAssignableFrom(obj.getClass())) {
				set.add(clazz.cast(obj));
			}
		}
		return set;
	}

	public static <E> Set<E> getNewSetFrom(Collection<E> collection) {
		if (CollectionUtils.isEmpty(collection)) {
			return Collections.emptySet();
		}
		return new HashSet<E>(collection);
	}
}
